﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Helpers
{
    public class ReflectionHelper
    {
        public static MethodInfo GetMethodInfoWithParent(string methodName, Type type)
        {
            MethodInfo result = null;

            //type.GetMethod(methodName,BindingFlags)

            return result;
        }

        public static T[] GetAttributes<T>(ICustomAttributeProvider obj, bool inheret)
            where T : Attribute
        {
            object[] attrs = obj.GetCustomAttributes(typeof(T), inheret);
            if (CollectionHelper.IsNullOrEmpty(attrs))
                return null;

            return attrs.ToList().Select(attr => (T)attr).ToArray();
        }

        public static T[] GetAttributes<T>(ICustomAttributeProvider obj)
            where T : Attribute
        {
            return GetAttributes<T>(obj, false);
        }

        public static T GetFirstAttribute<T>(ICustomAttributeProvider obj, bool inheret) 
            where T : Attribute
        {
            T[] attrs = GetAttributes<T>(obj, inheret);
            if (CollectionHelper.IsNullOrEmpty(attrs))
                return null;

            return attrs.First();
        }

        public static T GetFirstAttribute<T>(ICustomAttributeProvider obj)
            where T : Attribute
        {
            return GetFirstAttribute<T>(obj, false);
        }

        public static bool HasAttribute<T>(ICustomAttributeProvider obj, bool inheret)
            where T : Attribute
        {
            return !CollectionHelper.IsNullOrEmpty(GetAttributes<T>(obj, inheret));
        }

        public static bool HasAttribute<T>(ICustomAttributeProvider obj)
            where T : Attribute
        {
            return HasAttribute<T>(obj, false);
        }
    }
}
